package com.xyz.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.xyz.springcloud.entity.CommonResult;
import com.xyz.springcloud.entity.Payment;
import com.xyz.springcloud.myhandler.CustomerBlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 肖咉卓
 */
@RestController
public class RateLimitController {

    /**
     * @return
     */
    @GetMapping("/byResource")
    @SentinelResource(
            //名称唯一，随便什么都可以，一般填路径不要斜杆
            value ="byResource" ,
            //设置兜底方法
            blockHandler = "handleException"
    )
    public CommonResult byResource(){
        return new CommonResult(200,"按资源名称限流测试ok",new Payment(2020L,"serial001"));
    }

    public CommonResult handleException(BlockException exception){
        return new CommonResult(444,"按资源名称限流测试ok",exception.getClass().getCanonicalName()+"服务不可用");
    }

    @GetMapping("/rateLimit/byUrl")
    //@SentinelResource(value = "byUrl")
    public CommonResult byUrl(){
        return new CommonResult(200,"按Url限流测试ok",new Payment(2020L,"serial002"));
    }

    @GetMapping("/rateLimit/customerBlockHandler")
    @SentinelResource(
            value = "customerBlockHandler",
            //调用哪个类的兜底方法
            blockHandlerClass = CustomerBlockHandler.class,
            //调用哪个方法
            blockHandler = "handlerException2"
    )
    public CommonResult customerBlockHandler(){
        return new CommonResult(200,"按客户自定义",new Payment(2020L,"serial003"));
    }
}
